//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.toune.movie.base.myview;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.toune.movie.R;
import com.vondear.rxtool.RxDeviceTool;
import com.vondear.rxtool.RxImageTool;
import com.vondear.rxtool.model.ActionItem;

import java.util.ArrayList;

public class RxPopupSingleView extends PopupWindow {
    protected final int LIST_PADDING;
    private final int[] mLocation;
    private Context mContext;
    private Rect mRect;
    private int mScreenWidth;
    private int mScreenHeight;
    private boolean mIsDirty;
    private int popupGravity;
    private RxPopupSingleView.OnItemOnClickListener mItemOnClickListener;
    private ListView mListView;
    private ArrayList<ActionItem> mActionItems;
    private int colorItemText;

    public RxPopupSingleView(Context context) {
        this(context, -2, -2);
    }

    public RxPopupSingleView(Context context, int width, int height) {
        this.LIST_PADDING = 10;
        this.mLocation = new int[2];
        this.mRect = new Rect();
        this.popupGravity = 0;
        this.mActionItems = new ArrayList();
        this.colorItemText = 0;
        this.mContext = context;
        this.setFocusable(true);
        this.setTouchable(true);
        this.setOutsideTouchable(true);
        this.mScreenWidth = RxDeviceTool.getScreenWidth(this.mContext);
        this.mScreenHeight = RxDeviceTool.getScreenHeight(this.mContext);
        this.setWidth(width);
        this.setHeight(height);
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(LayoutInflater.from(this.mContext).inflate(R.layout.popupwindow_layout, (ViewGroup) null));
        this.initUI();
    }

    public RxPopupSingleView(Context context, int width, int height, int layout) {
        this.LIST_PADDING = 10;
        this.mLocation = new int[2];
        this.mRect = new Rect();
        this.popupGravity = 0;
        this.mActionItems = new ArrayList();
        this.colorItemText = 0;
        this.mContext = context;
        this.setFocusable(true);
        this.setTouchable(true);
        this.setOutsideTouchable(true);
        this.mScreenWidth = RxDeviceTool.getScreenWidth(this.mContext);
        this.mScreenHeight = RxDeviceTool.getScreenHeight(this.mContext);
        this.setWidth(width);
        this.setHeight(height);
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(LayoutInflater.from(this.mContext).inflate(layout, (ViewGroup) null));
        this.initUI();
    }

    private void initUI() {
        this.mListView = (ListView) this.getContentView().findViewById(R.id.title_list);
        this.mListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int index, long arg3) {
                RxPopupSingleView.this.dismiss();
                if (RxPopupSingleView.this.mItemOnClickListener != null) {
                    RxPopupSingleView.this.mItemOnClickListener.onItemClick((ActionItem) RxPopupSingleView.this.mActionItems.get(index), index);
                }

            }
        });
    }

    public void show(View view) {
        view.getLocationOnScreen(this.mLocation);
        this.mRect.set(this.mLocation[0], this.mLocation[1], this.mLocation[0] + view.getWidth(), this.mLocation[1] + view.getHeight());
        if (this.mIsDirty) {
            this.populateActions();
        }

        this.showAtLocation(view, this.popupGravity, this.mScreenWidth - 10 - this.getWidth() / 2, this.mRect.bottom + RxImageTool.dp2px(7.5F));
    }

    public void show(View view, int dex) {
        view.getLocationOnScreen(this.mLocation);
        this.mRect.set(this.mLocation[0], this.mLocation[1], this.mLocation[0] + view.getWidth(), this.mLocation[1] + view.getHeight());
        if (this.mIsDirty) {
            this.populateActions();
        }

        this.showAtLocation(view, this.popupGravity, this.mLocation[0], this.mRect.bottom + dex);
    }

    public void setColorItemText(int colorItemText) {
        this.colorItemText = colorItemText;
    }

    private void populateActions() {
        this.mIsDirty = false;
        this.mListView.setAdapter(new BaseAdapter() {
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView tv_itpop = null;
                ImageView iv_itpop = null;
                if (convertView == null) {
                    convertView = LayoutInflater.from(RxPopupSingleView.this.mContext).inflate(R.layout.item_single_popup, (ViewGroup) null);
                }

                tv_itpop = (TextView) convertView.findViewById(R.id.tv_itpop);
                tv_itpop.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                iv_itpop = (ImageView) convertView.findViewById(R.id.iv_itpop);
                if (RxPopupSingleView.this.colorItemText == 0) {
                    RxPopupSingleView.this.colorItemText = RxPopupSingleView.this.mContext.getResources().getColor(R.color.white);
                }

                tv_itpop.setTextColor(RxPopupSingleView.this.colorItemText);
                tv_itpop.setTextSize(14.0F);
                tv_itpop.setGravity(17);
                tv_itpop.setPadding(0, 10, 0, 10);
                tv_itpop.setSingleLine(true);
                ActionItem item = (ActionItem) RxPopupSingleView.this.mActionItems.get(position);
                tv_itpop.setText(item.mTitle);
//                if (item.mResourcesId == 0) {
//                    iv_itpop.setVisibility(8);
//                } else {
//                    iv_itpop.setVisibility(0);
//                    iv_itpop.setImageResource(item.mResourcesId);
//                }

                return convertView;
            }

            public long getItemId(int position) {
                return (long) position;
            }

            public Object getItem(int position) {
                return RxPopupSingleView.this.mActionItems.get(position);
            }

            public int getCount() {
                return RxPopupSingleView.this.mActionItems.size();
            }
        });
    }

    public void addAction(ActionItem action) {
        if (action != null) {
            this.mActionItems.add(action);
            this.mIsDirty = true;
        }

    }

    public void cleanAction() {
        if (this.mActionItems.isEmpty()) {
            this.mActionItems.clear();
            this.mIsDirty = true;
        }

    }

    public ActionItem getAction(int position) {
        return position >= 0 && position <= this.mActionItems.size() ? (ActionItem) this.mActionItems.get(position) : null;
    }

    public void setItemOnClickListener(RxPopupSingleView.OnItemOnClickListener onItemOnClickListener) {
        this.mItemOnClickListener = onItemOnClickListener;
    }

    public interface OnItemOnClickListener {
        void onItemClick(ActionItem var1, int var2);
    }
}
